package com.jd.xianghairui.jimdb;

import com.jd.jim.cli.Cluster;
import com.jd.jim.cli.JimClientFactory;
import com.jd.jim.cli.ReloadableJimClientFactory;

/**
 * 非Spring 环境的配置使用：
 * 注意：在系统推出时，调用ReloadableJimClientFactory的clear()方法
 */
public class JimClientDemo {

    public void close() {
        JimClientFactory.close();
    }

    public String getByKey(String key) {
        return JimClientFactory.getJimClinet().get(key);
    }

    private static class JimClientFactory {

        private static final ReloadableJimClientFactory clientFactory;
        private static final Cluster CLIENT_INSTANCE;

        static {
            clientFactory = new ReloadableJimClientFactory();
            clientFactory.setJimUrl("jim://1803528671997086613/2");

            CLIENT_INSTANCE = clientFactory.getClient();
        }

        public static Cluster getJimClinet() {
            return CLIENT_INSTANCE;
        }

        public static void close() {
            if(clientFactory != null){
                clientFactory.clear();
            }
        }
    }

}
